/*
* @(#)StandardVersionLabelRule.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.core;

import java.sql.Connection;
import java.util.List;

import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.dao.DmsVersionDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;

/**
 * <p>Class Name:     StandardVersionLabelRule.java  </p>
 * <p>Description:    The class mainly handle the version label increment logic.</p>
 *
 *  @author           Zoe Shum
 *  @company          DCIVision Limited
 *  @creation date    01/08/2003
 *  @version          $Revision: 1.7 $
 */

public class StandardVersionLabelRule implements VersionLabelRule {

  public static final String REVISION = "$Revision: 1.7 $";


  protected static final String CURRENT_VERSION_LABEL = "CURRENT";
  protected static final String LAST_OF_CURRENT = "LAST";
  protected static final String ROOT_VERSION_LABEL = "ROOT";
  protected static final String GENERAL_VERSION = "VERSION";

  private SessionContainer sessionContainer;
  private Connection conn;

  /**
   * Class constructor to create the instance of StandardVersionLabelRule
   * 
   * @param sessionContainer
   * @param conn
   */
  public StandardVersionLabelRule(SessionContainer sessionContainer, Connection conn) {
    this.conn = conn;
    this.sessionContainer = sessionContainer;
  }

  /**
   *  Release - release all variable in instance of DocumentRetrievalManager
    */
   public void release () {
     this.sessionContainer = null;
     this.conn = null;
   }

  /**
   * Update the version label of the all versions of specified dmsDocument
   * <P>
   * The latest version exists , the version label as StandardVersionLabelRule.LAST_OF_CURRENT
   * The versions that not is the latest and not the original version, the version label as 
   * StandardVersionLabelRule.GENERAL_VERSION
   * </P>
   * 
   * @param      version   the dmsVersion Object
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsVersion
   */
  public void process(DmsVersion version) throws ApplicationException {
    DmsVersionDAObject versionDAO = new DmsVersionDAObject(sessionContainer, conn);
    version.setVersionLabel(StandardVersionLabelRule.CURRENT_VERSION_LABEL);
    //List versionList = versionDAO.getByParentID(version.getParentID(), version.getDocumentID());
    List versionList = versionDAO.getVersionsListByDocumentID(version.getDocumentID(), "ASC"); //acending order
    if (versionList.size() > 1) {
      DmsVersion lastUpdateVersion = (DmsVersion) versionList.get(versionList.size() - 1);
      lastUpdateVersion.setVersionLabel(StandardVersionLabelRule.LAST_OF_CURRENT);
      versionDAO.updateObject(lastUpdateVersion);

      if (versionList.size() > 2) {
        DmsVersion generalVersion = (DmsVersion) versionList.get(versionList.size() - 2);
        generalVersion.setVersionLabel(StandardVersionLabelRule.GENERAL_VERSION);
        versionDAO.updateObject(generalVersion);
      }
    }
    versionDAO=null;
  }

  /**
   * Update the version label of the all versions of specified dmsDocument 
   * for minor checkin
   * 
   * <P>
   * The latest version exists , the version label as StandardVersionLabelRule.LAST_OF_CURRENT
   * The versions that not is the latest and not the original version, the version label as 
   * StandardVersionLabelRule.GENERAL_VERSION
   * </P>
   *
   * @param      version   the dmsVersion Object
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsVersion
   */
  public void processMinor(DmsVersion version) throws ApplicationException {
    DmsVersionDAObject versionDAO = new DmsVersionDAObject(sessionContainer, conn);
    version.setVersionLabel(StandardVersionLabelRule.CURRENT_VERSION_LABEL);
    List versionList = versionDAO.getParentVersionsListByParentIDByDocumentID(version.getParentID(), version.getDocumentID());
    if (versionList.size() > 1) {
      DmsVersion lastUpdateVersion = (DmsVersion) versionList.get(versionList.size() - 1);
      lastUpdateVersion.setVersionLabel(StandardVersionLabelRule.LAST_OF_CURRENT);
      versionDAO.updateObject(lastUpdateVersion);

      if (versionList.size() > 2) {
        DmsVersion generalVersion = (DmsVersion) versionList.get(versionList.size() - 2);
        generalVersion.setVersionLabel(StandardVersionLabelRule.GENERAL_VERSION);
        versionDAO.updateObject(generalVersion);
      }
    }
    versionDAO=null;
  }
}
